package com.application.controller;


import com.application.Exception.SystemException;
import com.application.domain.Activity;
import com.application.domain.User;
import com.application.domain.request;
import com.application.service.ActivityService;
import com.application.service.RequestService;
import com.application.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/activitis")
@Api(tags = "活动模块")
public class ActivityController {

    @Resource
    ActivityService activityService;

    @Resource
    RequestService requestService;

    @Resource
    UserService userService;


    @PutMapping("apply")
    @ApiOperation(value = "用户申请参加活动")
    public Result applyForActivity(@RequestParam String act_name, @RequestParam String openid) {
        int same = requestService.isSame(openid, act_name);
        if (same == 0) {
            User user = userService.selectById(openid);
            request request = new request(user.getUsername(), user.getTel(), openid);
            request.setName1(act_name);
            int save = requestService.save(request);
            String mes = save != 0 ? "申请成功，请耐心等待" : "申请失败";
            int code = save != 0 ? Code.SAVE_OK : Code.SAVE_ERR;
            return new Result(code, save, mes);
        }

        return new Result(Code.UPDATE_ERR, null, "请不要多次申请相同的活动");
    }


    @ApiOperation(value = "保存活动")
    @PostMapping
    public Result save(@RequestBody Activity activity, @RequestHeader(required = true) String openid) {

        User user = userService.selectById(openid);

        if (user.getLevel() >= 2) {
            //  这里根据  openid和act_name 来进行判断
            int save = activityService.saveActivity(activity);
            LocalDateTime start = activity.getStart();
            LocalDateTime deadline = activity.getDeadline();

            //  就是 开始时间等于或小于结束时间
            if(start.compareTo(deadline) >= 0){
                return new Result(Code.SAVE_ERR,null,"请正确选择时间");
            }
            String mes = save != 0 ? "保存数据成功" : "保存数据失败，请重新尝试";
            return new Result(save != 0 ? Code.SAVE_OK : Code.SAVE_ERR, save, mes);
        }

        return new Result(Code.SAVE_ERR, null, "权限不够");

    }

    @ApiOperation(value = "根据用户所属校区查看活动")
    @GetMapping("/getByUserSchool")
    public Result getByUserSchool(@RequestParam String openid){
        String school = userService.getSchool(openid);
        List<Activity> schools = activityService.getBySchool(school);
        String mes = school!=null? "查询成功":"查询失败";
        int code = schools !=null? Code.SELECT_OK:Code.SELECT_ERR;
        return new Result(code,schools,mes);
    }

    @ApiOperation(value = "根据校区查看活动")
    @GetMapping("getBySchool")
    public Result getBySchool(@RequestParam String school){
        List<Activity> bySchool = activityService.getBySchool(school);

        String mes  = bySchool !=null ? "查询成功":"查询失败，请重新尝试";
        int code = bySchool != null ? Code.SELECT_OK:Code.SELECT_ERR;
        return  new Result(code,bySchool,mes);
    }

    @ApiOperation(value = "获取活动信息")
    @GetMapping("/getById")
    public Result getById(@RequestParam String id) {
        //  这里需要更新查询语句
        Activity activity = activityService.selectById(id);
        String mes = activity != null ? "查询成功" : "查询失败，请重新尝试";
        return new Result(activity != null ? Code.SELECT_OK : Code.SELECT_ERR, activity, mes);
    }

    @ApiOperation(value = "获取全部活动")
    @GetMapping
    public Result getAll() {
        List<Activity> activities = activityService.selectAll();
        String mes = activities != null ? "查询成功" : "查询失败，请重新尝试";
        return new Result(activities != null ? Code.SELECT_OK : Code.SELECT_ERR, activities, mes);
    }

    @ApiOperation(value = "根据id删除活动")
    @DeleteMapping("/DeleteById")
    public Result deleteById(@RequestParam(required = true) String id, @RequestParam(required = true) String openid) {
        User user = userService.selectById(openid);
        if (user.getLevel() >= 2 && user.getCommunity().equals(activityService.selectById(id).getCommunity())) {
            int deleteById = activityService.deleteById(id);
            String mes = deleteById != 0 ? "删除成功" : "删除失败，请重新尝试";
            return new Result(deleteById != 0 ? Code.DELETE_OK : Code.DELETE_ERR, deleteById, mes);
        }

        return new Result(Code.DELETE_ERR, null, "权限不够");
    }

    @ApiOperation(value = "删除全部")
    @DeleteMapping
    public Result deleteAll() {
        if (1 != 1) {
            int deleteAll = activityService.deleteAll();
            String mes = deleteAll != 0 ? "删除成功" : "删除失败，请重新尝试";
            return new Result(deleteAll != 0 ? Code.DELETE_OK : Code.DELETE_ERR, deleteAll, mes);
        }
        return new Result(Code.DELETE_ERR, null, "想得美");
    }

    @DeleteMapping("/DeleteByList")
    public Result deleteByIds(@RequestParam List<String> ids) {
        int deleteByIds = activityService.deleteByIds(ids);
        String mes = deleteByIds != 0 ? "删除成功" : "删除失败，请重新尝试";
        return new Result(deleteByIds != 0 ? Code.DELETE_OK : Code.DELETE_ERR, deleteByIds, mes);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public Result updateById(@RequestBody Activity activity,
                             @RequestHeader(required = true) String openid,
                             @RequestHeader(required = true) String id
    ) {
        User user = userService.selectById(openid);
        if (user.getLevel() >= 2 && user.getCommunity().equals(activityService.selectById(id).getCommunity())) {
            int updateById = activityService.updateByIdActivity(activity);
            String mes = updateById != 0 ? "修改成功" : "修改失败，请重新尝试";
            int code = updateById != 0 ? Code.UPDATE_OK : Code.UPDATE_ERR;
            return new Result(code, updateById, mes);
        }
        return new Result(Code.DELETE_ERR, null, "权限不足");
    }


    @ApiOperation("图片确定存入数据库")
    @PostMapping("/download/commit")
    public Result DownLoadCommit(@RequestBody Activity activity,@RequestHeader(required = true)String openid) {
        User user = userService.selectById(openid);

        if(user.getLevel()>=2&&user.getCommunity().equals(activity.getCommunity())){
            int downLoad = activityService.DownLoad(activity);
            String mes = downLoad != 0 ? "保存成功" : "保存失败，请重新尝试";
            Integer code = downLoad != 0 ? Code.SAVE_OK : Code.SAVE_ERR;
            return new Result(code, downLoad, mes);
        }
        return new Result(Code.SAVE_ERR,null,"权限不够");
    }

    @ApiOperation(value = "根据点击量来查看热门的活动")
    @GetMapping("getByLikeclick")
    public Result getByClick(){
        List<Activity> byLikeclick = activityService.getByLikeclick();

        String mes = byLikeclick != null ? "查询成功":"查询失败，请重新尝试";
        int code = byLikeclick != null? Code.SELECT_OK:Code.SELECT_ERR;

        return new Result(code,byLikeclick,mes);
    }

}
